<template>
  <div class="page">
    <div class="panel">
      <div class="lottery" v-if="statistics">
        <div class="header">
          <h1>深山修炼</h1>
          <span>{{ statistics.openDay }}：{{ statistics.frequency }}只小猴，{{ statistics.num }}次修炼，{{ statistics.total }}个盘缠</span>
        </div>
        <div class="content">
          <div class="rules">
            <p class="title">选择自家小猴进入大山，向修炼场支付 <span>数量.猴子id</span> 的链克作为盘缠，完成修炼即可有概率的提升/降低小猴最低的一项属性值0.1，参与活动的小猴所有盘缠将会保存在四个山洞中，幸运的小猴将会得到洞中宝藏。</p>
            <p class="address" v-clipboard="`0x0a4d16e66e1939639f8a4ba6f92b67bccfe20d62`" @success="isCopy(1)" @error="isCopy(0)" :key="1">0x0a4d16e66e1939639f8a4ba6f92b67bccfe20d62</p>
            <p class="detail">
              修炼规则：
              <br>*本期活动，所有小猴均可参与修炼，无投食要求。
              <br>*四个山洞盘缠比例为1号5成、2号2成、3号1.5成、4号0.5成，剩余1成将会送入山外掘金场分享给其他小猴。
              <br>*小猴给完盘缠后会产生原始hash，最后四位数字为幸运代码，当幸运代码与神秘代码匹配后即可成功开启山洞大门，获得宝藏，多个小猴同时获得则根据数量所占比例平分宝藏。如神秘代码为：1234，幸运代码1234开启1号山洞，*234开启2号，**34开启3号，***4开启4号，单次修炼仅能获得最高宝藏。
              <br>*每次盘缠最少1个，最多50，单个账号一期最多参与50个盘缠，盘缠小数点后面数量转入余额，超额及小猴id不匹配则进入余额，不计入宝藏。
              <br>*每次修炼盘缠的加盐hash最后一位数字奇数增加，偶数降低小猴最低属性项0.1，若属性相同则随机一个。
              <br>*神秘代码由上证、深证开洞当日收盘价的两位小数依次组合得来。
              <br>*玩客猴将会拿出1月5日前所有分成收益，按照每期深山宝藏数量的10%投入山洞，用完为止，若有剩余，将用于下次宝藏活动。
              <br>*当天参与修炼的小猴将于次日16:00开启山洞宝藏，首次山洞打开时间为1月9日16:00。本次修炼活动最后一期参与截止时间为1月15日0:00，开启山洞时间为1月15日16:00。
              <br>*由于算法原因，暂不支持尾号为0的小猴参与修炼。
            </p>
          </div>
          <div class="history">
            <div class="title" @click="refresh">
              <span>寻宝记录</span>
              <i :class="{'refresh': refreshing}"></i>
            </div>
            <div class="wrap">
              <div class="item" v-for="(item,index) in luckyList" :key="index">
                <p class="line-1">{{ item.createTime | formatTime }} → <span>{{ item.coin }} wkc</span></p>
                <p class="line-3" v-clipboard="item.originalHash" @success="isCopy(1)" @error="isCopy(0)" :key="2">
                  <span>原始hash:<br>{{ item.originalHash }}</span>
                  <span style="color:red; margin-left:10px">{{ item.originalHashEnd }}</span>
                </p>
                <p class="line-3" v-clipboard="item.hash" @success="isCopy(1)" @error="isCopy(0)" :key="3">加盐hash:<br>{{ item.hash }}</p>
                <p class="line-4">==>
                  <router-link :to="`/monkey/${item.monkeyId}`"># {{ item.monkeyId }}</router-link>
                  <span v-if="item.monkeyGrow != 0">成长{{ item.monkeyGrow > 0 ? '+' : '' }}{{item.monkeyGrow}} </span>
                  <span v-if="item.monkeyBear != 0">生育{{ item.monkeyBear > 0 ? '+' : '' }}{{item.monkeyBear}} </span>
                  <span v-if="item.monkeyMoney != 0">掘金{{ item.monkeyMoney > 0 ? '+' : '' }}{{item.monkeyMoney}} </span>
                </p>
              </div>
              <infinite-loading @infinite="initData" ref="infiniteLoading2">
                <span slot="no-more"></span>
                <div slot="no-results">
                  <br>
                  <empty></empty>
                </div>
                <spinner slot="spinner" type="lines"></spinner>
              </infinite-loading>
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="panel" v-if="dayData.length > 0">
      <div class="title">寻宝结果</div>
      <div class="period">
        <div class="date">
          <div @click="filter(item.day,index)" :class="{'green': day == item.day}" v-for="(item,index) in dayData" :key="index">{{ item.day }}</div>
        </div>
        <div class="code" v-if="statistic">神秘代码：{{ statistic.index }}</div>
        <div class="statistic" v-if="statistic">{{ statistic.monkeyNum }}只小猴，{{ statistic.studyNum }}次修炼，{{ statistic.studyTotal }}个盘缠<br>添加10%，合计{{ statistic.total }}链克</div>
        <div class="num">
          <div @click="filter(1)" :class="{'green': grade == 1}">一号山洞</div>
          <div @click="filter(2)" :class="{'green': grade == 2}">二号山洞</div>
          <div @click="filter(3)" :class="{'green': grade == 3}">三号山洞</div>
          <div @click="filter(4)" :class="{'green': grade == 4}">四号山洞</div>
          <div @click="filter(-1)" :class="{'green': grade == -1}">我的</div>
        </div>
        <ul class="record">
          <li v-for="(item,index) in resultList" :key="index">
            <div>
              <span>原始hash： → #{{ item.monkeyId }}</span>
              <span class="green">{{ item.coin }}wkc</span>
            </div>
            <div>
              <span>{{ item.originalHash }}:</span>
              <span class="black">{{ item.originalHashEnd }}</span>
              <span class="red">→{{ item.amount }}wkc</span>
            </div>
            <div>钱包地址：{{ item.address }}</div>
          </li>
        </ul>
        <div v-show="resultList.length == 0" class="miss">噢哟~没有中奖噢</div>
      </div>
    </div>
  </div>
</template>

<script>
  import {
    Spinner
  } from 'vux'
  import empty from './common/empty.vue'
  import InfiniteLoading from 'vue-infinite-loading';
  export default {
    components: {
      empty,
      InfiniteLoading,
      Spinner
    },
    data() {
      return {
        isActive: false,
        current: 1,
        luckyList: [],
        statistics: null,
        refreshing: false,
        countdownTimer: null,
        dayData: [],
        statistic: null,
        day: '',
        grade: 1,
        resultList: []
      }
    },
    mounted() {
      this.$nextTick(function() {
        this.getStatistics()
        this.countdownTimer = setInterval(() => {
          this.getStatistics()
        }, 5000)
      });
      this.getListDay()
    },
    beforeDestroy() {
      clearInterval(this.countdownTimer);
    },
    methods: {
      initData($state) {
        this.$http
          .post(`/game/studyRecordPage`, {
            current: this.current
          })
          .then(res => {
            if (res.code == 200) {
              if (res.result.total > 0) {
                this.luckyList = this.luckyList.concat(res.result.records);
                $state.loaded();
                if (res.result.current < res.result.pages) {
                  this.current += 1;
                } else {
                  $state.complete();
                }
              } else {
                $state.complete();
              }
              setTimeout(() => {
                this.refreshing = false;
              }, 500)
            } else {}
          })
          .catch(function(error) {});
      },
      isBind() {
        var that = this;
        this.$http
          .get(`/users/isBind`)
          .then(res => {
            if (res.code == 200) {
              if (res.result) {} else {
                this.$vux.alert.show({
                  title: '提示',
                  content: '当前账户未绑定钱包地址，请重新登录完成绑定',
                  onHide() {
                    that.$router.replace('/login')
                  }
                })
              }
            } else {}
          })
          .catch(function(error) {
            clearInterval(timer)
          });
      },
      refresh() {
        this.refreshing = true;
        this.current = 1;
        this.luckyList = []
        this.$refs.infiniteLoading2.$emit('$InfiniteLoading:reset');
      },
      getStatistics() {
        this.$http
          .get(`/game/studyRecordStatistics`)
          .then(res => {
            if (res.code == 200) {
              this.statistics = res.result
              if (!res.result.open) {
                this.$router.replace('/404')
              }
            } else {}
          })
          .catch(function(error) {});
      },
      getListDay() {
        this.$http
          .get(`/game/studyListDay`)
          .then(res => {
            if (res.code == 200) {
              this.dayData = res.result
              this.statistic = res.result[0]
              this.day = res.result[0].day
              this.getStudyList()
            } else {}
          })
          .catch(function(error) {});
      },
      getStudyList() {
        this.$http
          .post(`/game/studyLuckRecord`, {
            day: this.day,
            grade: this.grade
          })
          .then(res => {
            if (res.code == 200) {
              this.resultList = res.result
              // this.statistic = res.result[0]
            } else {}
          })
          .catch(function(error) {});
      },
      filter(param, index) {
        if (typeof(index) == 'number') {
          this.statistic = this.dayData[index]
        }
        if (typeof(param) == 'string') {
          this.day = param
        } else {
          this.grade = param
        }
        this.getStudyList()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .page {
    padding-top: 12px;
  }
  .lottery {
    overflow: hidden;
  }
  .header {
    text-align: center;
    h1 {
      font-size: 20px;
      color: $tmdark;
    }
    span {
      font-size: 14px;
      color: $grey;
    }
  }
  .content {
    border-top: 1px solid #dfdfdf;
    margin-top: 10px;
    transition: all .5s ease .2s;
    .rules {
      .title {
        font-size: 12px;
        color: $gray;
        text-align: left;
        span {
          color: $red;
        }
      }
      .address {
        font-size: 14px;
        color: $green;
        word-break: break-all;
        line-height: 1;
        padding: 15px 0;
      }
      .detail {
        font-size: 10px;
        color: $grey;
        span {
          color: $red;
        }
      }
    }
  }
  .title {
    font-size: 16px;
    color: #6c7ea3;
    text-align: center;
    padding: 15px 0;
    span {
      vertical-align: middle;
    }
    i {
      display: inline-block;
      vertical-align: middle;
      width: 16px;
      height: 16px;
      background: url() no-repeat;
      background-size: 100%;
      &.refresh {
        animation: rotating 1s linear infinite;
      }
    }
  }
  .history {
    .wrap {
      max-height: 600px;
      overflow-y: scroll;
      &::-webkit-scrollbar {
        display: none;
      }
    }
    .item {
      border-bottom: 1px solid #dfdfdf;
      padding: 5px 0;
      .line-1 {
        font-size: 10px;
        span {
          color: $red;
        }
      }
      .line-2 {
        font-size: 12px;
        color: #6c7ea3;
        word-break: break-all;
      }
      .line-3 {
        font-size: 10px;
        color: $grey;
        word-break: break-all;
      }
      .line-4 {
        font-size: 12px;
        a {
          color: $link;
        }
      }
    }
  }
  .period {
    .date {
      div {
        display: inline-block;
        margin-right: 10px;
      }
    }
    .code {
      color: $grey;
    }
    .statistic {
      line-height: 1.2;
      padding: 5px 0;
      font-size: 12px;
    }
    .num {
      div {
        display: inline-block;
        margin-right: 10px;
        color: #666;
        font-size: 14px;
      }
    }
    .record {
      li {
        border-bottom: 1px solid #eee;
        line-height: 1.2;
        color: $grey;
        font-size: 12px;
        padding: 5px 0;
        word-break: break-all;
        .red {
          color: $red;
        }
        .black {
          color: #000;
        }
        div {
          &:last-child {
            margin-top: 5px;
          }
        }
      }
    }
    .miss {
      text-align: center;
      padding: 20px 0;
    }
  }
</style>

<style>
  @keyframes rotating {
    from {
      transform: rotate(0)
    }
    to {
      transform: rotate(360deg)
    }
  }
</style>
